package com.situ.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.BanJi;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.entity.User;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.StudentServiceImpl;
import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration;

import net.sf.json.JSONArray;

public class StudentServlet extends HttpServlet {
	private IStudentService studentService = new StudentServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=utf-8");
		String method = req.getParameter("method");
		System.out.println(method);
		switch (method) {
		case "pageList":
			pageList(req,resp);
			break;
		case "addStudent":
			addStudent(req,resp);
			break;
		case "selectBanji":
			selectBanji(req,resp);
			break;
		case "toUpdate":
			toUpdate(req,resp);
			break;
		case "update":
			update(req,resp);
			break;
		case "delete":
			delete(req,resp);
			break;

		default:
			break;
		}
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		boolean result = false;
		result = studentService.delete(Integer.parseInt(id));
		if (result) {
			resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		}
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banji");
		Student student = new Student(Integer.parseInt(id),name, Integer.parseInt(age), gender, Integer.parseInt(banjiId));
		boolean result = false;
		result = studentService.update(student);
		if (result) {
			resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		} else {
			resp.sendRedirect(req.getContextPath() + "/fail.jsp");
		}
	}

	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		Student student = new Student(Integer.parseInt(id));
		Student student2 = studentService.toUpdate(student);
		System.out.println(student2);
		req.setAttribute("student", student2);
		req.getRequestDispatcher("/toupdatestudent.jsp").forward(req, resp);
	}

	private void selectBanji(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<BanJi> list = studentService.selectBanji();
		for (BanJi banJi : list) {
			System.out.println("zheshibanji"+banJi);
		}
		resp.setContentType("text/html;charset=utf-8");
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

	private void addStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=utf-8");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banjiId");
		System.out.println(name);
		System.out.println(age);
		System.out.println(gender);
		System.out.println(banjiId);
		boolean result = false;
		Student student = new Student(name, Integer.parseInt(age), gender, Integer.parseInt(banjiId));
		result = studentService.addStudent(student);
		if (result) {
			resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		} else {
			resp.sendRedirect(req.getContextPath() + "/fail.jsp");
		}
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user = (User)req.getSession().getAttribute("user");
		if (null == user) {
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
			return;
		}
		String stuPageNo = req.getParameter("pageNo");
		int pageNo = (stuPageNo == null)? 1 : Integer.parseInt(stuPageNo);
		String stuPageSize = req.getParameter("pageSize");
		int pageSize = (stuPageSize == null)? 3 : Integer.parseInt(stuPageSize);
		PageBean pageBean = studentService.pageList(pageNo,pageSize);
		System.out.println(pageBean);
		req.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("/student_page_list.jsp").forward(req, resp);
	}

}
